/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.controllers;

import com.ntb.common.SecuredAction;
import com.ntb.controllers.forms.DashboardForm;
import com.ntb.models.BuildingsDAO;
import com.ntb.models.ContactDAO;
import com.ntb.models.FlatsDAO;
import com.ntb.models.LandsDAO;
import com.ntb.models.SalesDAO;
import com.ntb.models.impl.BuildingsDAOImpl;
import com.ntb.models.impl.ContactDAOImpl;
import com.ntb.models.impl.FlatsDAOImpl;
import com.ntb.models.impl.LandsDAOImpl;
import com.ntb.models.impl.SalesDAOImpl;
import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Thinh Hung
 */
public class DashboardAction extends SecuredAction {

    @Override
    public ActionForward ntbExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        DashboardForm dashboardForm = (DashboardForm) form;

        LandsDAO landDao = new LandsDAOImpl();
        BuildingsDAO buildingDao = new BuildingsDAOImpl();
        FlatsDAO flatDao = new FlatsDAOImpl();
        SalesDAO saleDao = new SalesDAOImpl();
        ContactDAO contactDao = new ContactDAOImpl();

        List yearList = new ArrayList();
        Calendar cal = new GregorianCalendar();
        int currentYear = cal.get(Calendar.YEAR);
        while (currentYear >= 2005) {
            yearList.add(currentYear);
            currentYear--;
        }
        request.setAttribute("yearList", yearList.iterator());
        int year = Integer.parseInt(yearList.get(0).toString());
        if (dashboardForm.getYear() >= 2005) {
            year = dashboardForm.getYear();
        }

        String[] shortMonths = new DateFormatSymbols().getShortMonths();
        request.setAttribute("monthList", shortMonths);
        request.setAttribute("countLandList", landDao.getCountList(year));
        request.setAttribute("countBuildingList", buildingDao.getCountList(year));
        request.setAttribute("countSaleList", saleDao.getCountList(year));

        request.setAttribute("contactList", contactDao.getLast(5));

        ArrayList countList = new ArrayList();
        countList.add(new String[]{"Total lands", landDao.countAll() + ""});
        countList.add(new String[]{"Total buildings", buildingDao.countAll() + ""});
        countList.add(new String[]{"Total flats", flatDao.countAll() + ""});
        countList.add(new String[]{"Total sales", saleDao.countAll() + ""});
        countList.add(new String[]{"Total contacts", contactDao.countAll() + ""});
        request.setAttribute("countList", countList);

        return mapping.getInputForward();
    }
}